package com.ruoyi.business.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.business.service.IAppointmentService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AppointmentMapper;
import com.ruoyi.business.domain.Appointment;

import com.ruoyi.common.core.text.Convert;

/**
 * 养修信息预约Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-25
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;

    /**
     * 查询养修信息预约
     * 
     * @param id 养修信息预约主键
     * @return 养修信息预约
     */
    @Override
    public Appointment selectAppointmentById(Long id) {
        return appointmentMapper.selectAppointmentById(id);
    }

    /**
     * 查询养修信息预约列表
     * 
     * @param appointment 养修信息预约
     * @return 养修信息预约
     */
    @Override
    public List<Appointment> selectAppointmentList(Appointment appointment) {
        return appointmentMapper.selectAppointmentList(appointment);
    }

    /**
     * 新增养修信息预约
     * 
     * @param appointment 养修信息预约
     * @return 结果
     */
    @Override
    public int insertAppointment(Appointment appointment) {
        appointment.setCreateTime(DateUtils.getNowDate());
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        return appointmentMapper.insertAppointment(appointment);
    }

    /**
     * 修改养修信息预约
     * 
     * @param appointment 养修信息预约
     * @return 结果
     */
    @Override
    public int updateAppointment(Appointment appointment) {
        //到数据库查询状态
        Appointment currentApp = appointmentMapper.selectAppointmentById(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(currentApp.getStatus())){
            //不是预约状态不能进行修改
            throw new ServiceException("不在预约中,不能进行编辑!");
        }
        appointment.setStatus(Appointment.STATUS_APPOINTMENT);
        return appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 批量删除养修信息预约
     * 
     * @param ids 需要删除的养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByIds(String ids) {
        return appointmentMapper.deleteAppointmentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除养修信息预约信息
     * @param id 养修信息预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentById(Long id) {
        return appointmentMapper.deleteAppointmentById(id);
    }


    /**
     * 客户到店
     * @param id 预约单id
     */
    @Override
    public void updateStatusToShop(Long id) {
        Appointment appointment = appointmentMapper.selectAppointmentById(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new ServiceException("只有处于预约中的才能到店操作");
        }
        appointment.setStatus(Appointment.STATUS_ARRIVAL);
        appointment.setActualArrivalTime(new Date());
        appointmentMapper.updateAppointment(appointment);
    }

    @Override
    public void updateStatusToCancel(Long id) {
        Appointment appointment = appointmentMapper.selectAppointmentById(id);
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())){
            throw new ServiceException("只有处于预约中的才能进行取消");
        }
        appointmentMapper.updateStatus(id,Appointment.STATUS_CANCEL);
    }
}
